package com.example.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.UUID;
import java.util.concurrent.*;

/**
 * 线程池配置
 */
@Configuration
public class ThreadPoolConfig {

    @Bean(name = "threadPoolExecutor")
    public ThreadPoolExecutor getThreadPoolExecutor() {
        return new ThreadPoolExecutor(10,
                10,
                // 非核心线程存活时间
                5000,
                // 单位：毫秒-MILLISECONDS、秒-SECONDS
                TimeUnit.MILLISECONDS,
                new ArrayBlockingQueue<>(20),
                // 线程池前缀
                r -> {
                    Thread thread = new Thread(r);
                    thread.setName("thread-pool-" + UUID.randomUUID());
                    return thread;
                },
                (r, e) -> {
                    throw new RejectedExecutionException("线程池已满");
                });
    }

}
